---
title: "Appendix to Carsten Q. Schneider (2023): Set-Theoretic Multi-Method Research:
  A Guide to Combining QCA and Case Studies, Cambridge University Press"
author: "Carsten Q. Schneider"
date: "`r format(Sys.time(), '%B %d, %Y | %H:%M:%S | %Z')`"
output:
  html_document:
    toc: yes
    number_sections: yes
    highlight: tango
    theme: cosmo
    code_folding: show
  word_document:
    toc: yes
    number_sections: yes
  pdf_document: default
bibliography: book.bib
---


```{r setup, include = FALSE, warning=FALSE, message=FALSE, comment=NA, results='hide'}
# Setup chunk
knitr::opts_chunk$set(echo = TRUE,
                      warning = FALSE,
                      message = FALSE,
                      comment = NA)
options(knitr.kable.NA = "",
        tinytex.verbose = TRUE)

library(pacman)
p_load(QCA, SetMethods, xtable, stargazer, tidyr, tidyverse, scales, countrycode,
       ggthemes, kableExtra, knitr, magrittr, dplr, stringr, data.table, reshape2)
```

*This document contains all the analyses presented in the book. Beyond this, additional, auxiliary analyses are provided, mostly to further analyze the properties of the data (skeweness, simplifying assumptions etc.) and to visualize the QCA solution formulas.*

*All replication files (datasets in csv format and an RMarkdown file that produces this document) can be found in the Harvard Dataverse at https://doi.org/10.7910/DVN/URMOVC *

*System setting and parameters under which the analyses were performed:*

* MacOS: Sonoma 14.0
* R: 4.2.2 "Innocent and Trusting"
* RStudio: 2023.06.0+421  


# Chapter 1: @Schneider2014

The data come from Carsten Q. Schneider and Kristin Makszin 2014 article "Forms of welfare capitalism and education-based participatory inequality." [https://doi.org/10.1093/ser/mwu010] The paper analyses whether the degree of political inequality between social groups is shaped by features of the welfare capitalist system. This example is used in chapters 1 and 5.

*Outcome:*

* `LPI` = Low participatory inequality


*Conditions:*

* `WC` = High wage coordination

* `UN` = High union density

* `EP` = High employment protection

* `LM` = High labor market expenditure


## Loading and checking the data for skeweness and ambiguous cases

To begin with, we load the data and check for skeweness and the presence of ambiguous cases (that is, cases with 0.5 fuzzy set membership score in a condition or the outcome set).

```{r chap 1 read and check data}
MACRO.d <- read.csv('SchneiderMakszin2014.csv', row.names = 1)

head(MACRO.d)

# conditions
conds <- c("WC", "UN", "EP", "LM")


# data diagnostics
skew.check(data = MACRO.d,
           hist = TRUE)


# ambiguous cases
ambig.cases(MACRO.d)
# BG05  in high employment protection
```

No single set is too skewed. Bulgaria in 2005 holds a membership of 0.5 in condition high employment protection.


## Analysis of sufficiency, outcome $LPI$

In a first step, we produce a truth table.

```{r chap 1 truth table}
# truth table
tt_y <- truthTable(data = MACRO.d,
                   outcome = 'LPI',
                   conditions = conds,
                   incl.cut = 0.8,
                   sort.by = c('OUT', 'incl'),
                   complete = TRUE,
                   show.cases = TRUE)
tt_y
# only one logical remainder
```

We logically minimize the truth table to obtain the most parsimonious solution, reveal the simplifying assumptions, and display the XY plots of the solution.

```{r chap 1 most parsimonious solution}
# most parsimonious solution
sol_yp <- minimize(input = tt_y,
                   include = '?',
                   details = TRUE)
sol_yp

# simplifying assumptions
sol_yp$SA

# XY plots
pimplot(data = MACRO.d,
        outcome = 'LPI',
        results = sol_yp,
        all_labels = TRUE,
        jitter = TRUE)
```


We establish each case's membership in *supportive welfare regimes* by intersecting their membership scores in the conjunctions $LM*WC$ and $LM*EP$. We then display the result in an XY plot. It corresponds to the one shown as figure 1.2 in the book.

```{r chap 1 supportive regime}
# create XY plot for supportive welfare regime LM(WC+EP)
MACRO.d$SUP1 <- with(MACRO.d, fuzzyor(WC, EP))

MACRO.d$SUPPORT <- with(MACRO.d, fuzzyand(LM, SUP1))

xy.plot(data = MACRO.d,
        x = 'SUPPORT',
        y = 'LPI',
        xlab = 'supportive welfare regimes',
        ylab = 'low participatory inequality',
        fontsize = 2,
        jitter = TRUE)
```

# Chapter 2: @Vis2009EJPR

In chapter 2, I use the example by @Vis2009EJPR. The solution formula consists of a single set. We can therefore postpone all those challenges for SMMR that arise from disjunctions (equifinality) and conjunctions (conjunctural causation).

*Outcome*

- `U` = Unpopular welfare state reforms

*Conditions*

* `P` = Weak political position

* `S` = Weak socio-economic position

* `R` = Right-leaning government


I start by producing the solution formula and then turn first to single-case and then to comparative-case SMMR designs.

## Producing the solution formula

We begin by loading the data

```{r chap 2 loading data}
VIS_fs <- read.csv("Vis_09_fs.csv", row.names = 1)
VIS_fs

tt_fy <- truthTable(data = VIS_fs,
                    outcome = "U",
                    conditions = "P, S, R",
                    incl.cut = 0.8,
                    sort.by = "incl",
                    complete = TRUE)
tt_fy
```


Based on this truth table, we obtain the following most parsimonious solution formula.

```{r chap 2 solution formula}
sol_fyp <- minimize(tt_fy, 
                    include = "?", 
                    details = TRUE)
sol_fyp
```

The single condition 'socio-economic trouble' $S$ is found to be sufficient for outcome 'unpopular reforms $U$. 

The solution can be visualized with an XY plot.

```{r chap 2 XY plot}
pimplot(data = VIS_fs,
        outcome = "U",
        results = sol_fyp,
        jitter = TRUE,
        all_labels = TRUE)
```

If the data was crisp, we could also use the `pimplot()` command and add the argument `crisp = TRUE`.

```{r chap 2 xyplot crisp}
pimplot(data = VIS_fs,
        outcome = "U",
        results = sol_fyp,
        jitter = TRUE,
        all_labels = TRUE,
        crisp = TRUE)
```

## Single-case SMMR designs

There are two model-refining single-case designs: the study of a deviant coverage case for identifying a missing disjunct, on the one hand, and the study of a deviant consistency case for identifying a missing conjunct.

```{r chap 2 dvoc}
dcov <- smmr(results = sol_fyp, 
             outcome = "U", 
             match = FALSE, 
             cases = 4)
dcov
```

```{r chap 2 dcons}
dcons <- smmr(results = sol_fyp, 
              outcome = "U", 
              match = FALSE, 
              cases = 3)
dcons
```

For causal inference SMMR designs, one option exists: the study of a typical case.

```{r chap 2 typ}
typ_term <- smmr(results = sol_fyp, 
                 outcome = "U", 
                 match = FALSE, 
                 cases = 1)
typ_term
```


## Comparative-case SMMR designs

There are two comparative-case descriptive inference SMMR designs. The first matches deviant coverage cases with iir cases with the goal of identifying missing disjuncts.

```{r chap 2 dvoc iir}
dcoviir <- smmr(results = sol_fyp, 
                outcome = "U", 
                match = TRUE, 
                cases = 4)
dcoviir
```

The second design matches typical cases with deviant consistency cases with the goal of identifying  missing conjuncts.

```{r chap 2 typ dcons}
typdcons <- smmr(results = sol_fyp, 
                 outcome = "U", 
                 match = TRUE, 
                 cases = 3)
typdcons
```


For causal inference, there are two comparative-case SMMR designs. The first matches typical cases with iir cases.

```{r chap 2 typiir}
typiir_term <- smmr(results = sol_fyp, 
                    outcome = "U", 
                    match = TRUE, 
                    cases = 6, 
                    max_pairs = 10)
typiir_term
```

The second matches the most typical with the just-so typical case.

```{r chap 2 typtyp}
typtyp_term <- smmr(results = sol_fyp, 
                    outcome = "U", 
                    match = TRUE, 
                    cases = 5, 
                    max_pairs = 10)
typtyp_term
```


# Chapter 3: @Stevens2016

In chapter 3, the study by @Stevens2016 on corruption perception is used to illustrate how SMMR works in the presence of disjunctions (without conjunctions). 

*Outcome*

* `HC` = High perceived corruption 

*Conditions*

* `LD` = Low democracy

* `HR` = High rational secular orientation

* `HS` = High self expression orientation

* `LH` = Low human development

* `HI` = High income inequality


To bring to the fore the challenges and the solutions provided, I do alter the data by @Stevens2016 in several regards and as outlined in the following code.

```{r chap 3 changing data}
# Get original data and manipulate to produce altered data set ####
STEVENS <- read.csv(file = 'Stevens_16_fs.csv', 
                 sep = ';', 
                 dec = ',',
                 row.names = 1)


STEVENS$D <- 1-STEVENS$D  # invert scale so that the presence of condition appears in sol
STEVENS$H <- 1-STEVENS$H  # invert scale so that the presence of condition appears in sol

# turn set labels from one-letter to two letter
names(STEVENS) <- c("LD", "HR", "HS", "LH", "HI", "HC")

# recalibrate specific cases to have more deviant cases consistency
STEVENS['Jordan',1] <- 0.7
STEVENS['Georgia',1] <- 0.6
STEVENS['Kazakhstan',1] <- 0.95
STEVENS['Ethiopia',1] <- 0.83
STEVENS['Belarus',1] <- 0.9
STEVENS['Vietnam',1] <- 0.85
STEVENS['Pakistan',4] <- 0.9
STEVENS['Tanzania',4] <- 0.89
STEVENS['Morocco',4] <- 0.62
STEVENS['Rwanda',4] <- 0.903

write.csv(STEVENS, file = 'Stevens_16_fs_altered.csv')
```


With the altered data set, I first produce the truth table and the most parsimonious solution, which is also visualized via XY plots.

```{r chap 3 truthtable mostpars}
STEV <- read.csv('Stevens_16_fs_altered.csv', row.names = 1)

# object containing the condition labels
conds <- c("LD", "HR", "HS", "LH", "HI")

# truth table
TT_y <- truthTable(data = STEV,
           outcome = 'HC',
           conditions = conds,
           incl.cut = 0.85,
           show.cases = TRUE,
           n.cut = 2,
           sort.by = c('OUT', 'incl'),
           complete = TRUE)
TT_y
# a clear cutoff, both in terms of consistency and PRI

# most parsimonious solution
sol_yp <- minimize(TT_y,
                   details = TRUE,
                   include = '?')
sol_yp

# xy plot
pimplot(data = STEV,
        outcome = 'HC',
        results = sol_yp,
        jitter = TRUE,
        all_labels = TRUE)
```


## Descriptive inference SMMR

I start with descriptive inference SMMR designs. To identify missing disjuncts, two designs are available: the study of a single deviant coverage case or the comparison of this case type with an iir case.


```{r chap 3 dvoc dcoviir}
# deviant coverage case
dcov <- smmr(results = sol_yp, 
             outcome = "HC", 
             match = FALSE, 
             cases = 4)
dcov

# deviant coverage - iir case
dcoviir <- smmr(results = sol_yp, 
                outcome = "HC",
                match = TRUE, 
                cases = 4)
dcoviir
```

To identify omitted conjuncts, also two options exist: the within-case analysis of a single deviant consistency case or a comparison of this case type with a typical case.

```{r chap 3 dcons dcons-typ}
dcons <- smmr(results = sol_yp, 
              outcome = "HC", 
              match = FALSE, 
              cases = 3)
dcons

typdcons <- smmr(results = sol_yp, 
                 outcome = "HC", 
                 match = TRUE, 
                 cases = 3, 
                 max_pairs = 10)
typdcons
```

## Causal inference SMMR

There are three causal inference SMMR designs: within-case analysis of a single typical case, the comparison with an iir case, and the comparison between two typical cases.

```{r chap 3 typ}
typ_term <- smmr(results = sol_yp, 
                 outcome = "HC", 
                 match = FALSE, 
                 cases = 1)
typ_term
```

```{r chap 3 typiir}
typiir_term <- smmr(results = sol_yp, 
                    outcome = "HC", 
                    match = TRUE, 
                    cases = 6, 
                    max_pairs = 10)
typiir_term
```

```{r chap 3 typtyp}
typtyp_term <- smmr(results = sol_yp, 
                    outcome = "HC", 
                    match = TRUE, 
                    cases = 5, 
                    max_pairs = 10)
typtyp_term
```


# Chapter 4: @Haesebrouck2020

In chapter 4, I discuss the challenges for SMMR that arise from the presence of conjunctions (without disjunctions). For this, I use the example by @Haesebrouck2020 on decisions to deploy military abroad.

*Outcome*

- `~PC` = No political contestation

*Conditions*

- `PD` = Not potentially divisive operation (PD)

- `HR` = High risk operation

- `PI` = Parliament involved

- `PF` = Parliament fractionalized

- `RO` = Right opposition

- `GS` = Government strong

- `GP` = Government polarized

Just as in the previous chapter, also in this example, I alter the data slightly such that the challenges to SMMR triggered by conjunctions are brought to the fore. The following code provides the details of the changes to the data.

```{r chap 4 alter data}
# Adjusting data for didactical purposes
HAES.d <- read.csv('data_fs.csv', row.names = 1)

HAES.d['ITA_Alba', 5] <- 0.8
HAES.d['FRN_Sangaris', 5] <- 0.885
HAES.d['FRN_DaeshIr', 5] <- 0.9
HAES.d['SLK_EUMali', 5] <- 0.93
HAES.d['SLK_Sophia', 1] <- 0.87 
HAES.d['DK_Kosovo', 1] <- 0.6 

HAES.d['BEL_DaeshIr', 4] <- 0.1
HAES.d['DK_Serval', 4] <- 0.01
HAES.d['FRN_Serval', 4] <- 0.02

HAES.d$PD <- 1 - HAES.d$PD

write.csv(HAES.d, "HaesebrouckImmerseel_20_data_fs_altered.csv")

HAES.data <- read.csv('HaesebrouckImmerseel_20_data_fs_altered.csv', row.names = 1)
```

I produce the truth table, the most parsimonious solution, and the XY plots for this solution

```{r chap 4 tt mostpars xyplots}
# truth table
TT_yn <- truthTable(data = HAES.data, outcome = "~PC",
                   conditions = c("PD", "HR", "PI","PF", "RO", "GP"),
                   incl.cut = 0.75,
                   show.cases = TRUE,
                   complete = FALSE,
                   sort.by = c("OUT", "incl"))

TT_yn

# most parsimonious solution
sol_nyp <- minimize(TT_yn, 
                  include = "?", 
                  details = TRUE)
sol_nyp

# XY plots
pimplot(data = HAES.data,
        outcome = "PC",
        results = sol_nyp,
        jitter = TRUE, 
        all_labels = TRUE)
```


## Descriptive inference SMMR

The two SMMR designs for identifying missing disjuncts are implemented as follows.

```{r chap 4 dvoc dcoviir}
# deviant coverage
dcov <- smmr(results = sol_nyp, 
             outcome = "~PC", 
             match = FALSE, 
             cases = 4)
dcov

# deviant coverage - iir
dcoviir <- smmr(results = sol_nyp, 
                outcome = "~PC", 
                match = TRUE, 
                cases = 4)
dcoviir
```


The two SMMR designs for identifying missing conjuncts are implemented as follows:

```{r chap 4 dcons dconstyp}
# deviant consistency
dcons <- smmr(results = sol_nyp, 
              outcome = "~PC", 
              match = FALSE, 
              cases = 3)
dcons

# deviant consistency - typical
typdcons <- smmr(results = sol_nyp, 
                 outcome = "~PC", 
                 match = TRUE, 
                 cases = 3)
typdcons
```


## Causal inference SMMR

The presence of conjuncts requires researchers to make a choice when aiming at causal inferences: do they want to solicit conceptual arguments and treat the conjunction as one set or do they want to treat each conjunct in the conjunction separately. The latter is the default options. The following code implements the three SMMR designs that operate on focal conjuncts.

```{r chap 4 foc typ typiir typtyp}
# typical
typ_foc <- smmr(results = sol_nyp, 
                outcome = "~PC", 
                match = FALSE, 
                cases = 2)
typ_foc

# typical - iir
typiir_foc <- smmr(results = sol_nyp, 
                   outcome = "~PC", 
                   match = TRUE, 
                   cases = 2, 
                   max_pairs = 10)
typiir_foc

# typical - typical
typtyp_foc <- smmr(results = sol_nyp, 
                   outcome = "~PC",
                   match = TRUE, 
                   cases = 1)
typtyp_foc
```

If a researcher decides that good reasons exist to move up on the ladder of abstraction and treat the conjunction (in the current example $PD*RO$) as one set, then the following code implements this choice.

```{r chap 4 term typ typiir typtyp}
# typical
typ_term <- smmr(results = sol_nyp, 
                outcome = "~PC", 
                match = FALSE, 
                cases = 1)
typ_term

# typical - iir
typiir_term <- smmr(results = sol_nyp, 
                   outcome = "~PC", 
                   match = TRUE, 
                   cases = 6, 
                   max_pairs = 10)
typiir_term

# typical - typical
typtyp_term <- smmr(results = sol_nyp, 
                   outcome = "~PC",
                   match = TRUE, 
                   cases = 5)
typtyp_term
```

## Chapter 4: @SchneiderSchulze2010, necessary INUS condition

Sometimes, INUS conditions qualify as necessary conditions for the outcome. This must be taken into account in all causal inference SMMR designs that zoom in on the necessary focal conjunct. This is done by specifying the necessary INUS condition via the argument `nec.cond = ` in function `smmr()`. For illustration I use the study by @SchneiderSchulze2010.


*Outcome* 

* `EXPORT` = success in export of high-tech products

*Conditions*

* `EMP`= high employment protection 

* `BARGAIN` = high collective bargaining

* `UNI` = a lot of university training

* `OCCUP` = a lot of occupational training

* `STOCK` = big stock market size

* `MA` = a lot of mergers and acquisitions


```{r chap 4 read data}
SCHF <- read.csv("Schneider_etal_10_fs.csv", row.names = 1)
```


We identify the necessary condition $STOCK$ and visualize it in an XY plot.

```{r chap 4 necessary condition}
# Identify necessary condition
NEC <- superSubset(SCHF, outcome = "EXPORT",
            conditions = c("EMP","BARGAIN","UNI","OCCUP","STOCK", "MA"),
            incl.cut = .87, 
            ron.cut = 0.5,
            cov.cut = 0.6,
            depth = 1)

NEC

# Plot necessary condition

pimplot(SCHF,
        results = NEC,
        outcome = "EXPORT",
        necessity = TRUE)
```


In the next step, we identify the sufficient conditions. For this, we first produce the truth table. Since we do have a necessary condition, we must make sure that our sufficiency statements do not contradict this necessity claim. For this, we apply the Enhanced Standard Analysis (ESA). This means, we must block from being included in the logical minimization all those truth table rows that would contradict the necessity claim.

```{r chap 4 truth table}
# Obtain truth table
TT_y <- truthTable(SCHF, outcome = "EXPORT",
                   conditions = c("EMP","BARGAIN","UNI","OCCUP","STOCK", "MA"),
                   incl.cut = .9,
                   pri.cut = 0.5,
                   sort.by =c('OUT', 'incl'))
TT_y
```

Since we do have a necessary condition, we must make sure that our sufficiency statements do not contradict this necessity claim. For this, we apply the Enhanced Standard Analysis (ESA). This means, we must block from being included in the logical minimization all those truth table rows that would contradict the necessity claim.

```{r chap 4 enhanced truth table}
# Perform ESA by blocking all truth table rows that contain the negation of the necessary condition STOCK
TT_yesa <- esa(oldtt = TT_y,
               nec_cond = "STOCK")
TT_yesa
```


The logical minimization produces two enhanced most parsimonious solutions (aka model ambiguity). For illustration I focus on model 1. The insights and arguments on SMMR that follow remain unaffected by the presence of model ambiguity and the choice of model 1 for illustration.

```{r chap 4 enhanced most parsimonious solution}
# Logically minimize the enhanced truth table to obtain the enhanced most parsimonious solution
sol_yp <- minimize(TT_yesa,
                      include = "?", 					   
                      details = TRUE, show.cases = TRUE)

sol_yp
```


The empirical illustration consists of the three causal inference SMMR designs because only for them the focal conjunct principle applies. I illustrate using term 3 ($\sim OCCOP*STOCK*\sim MA$).

The best available typical case is found as follows. Note that argument `nec.cond =` specifies $STOCK$ as the necessary INUS condition.

```{r chap 4 typ}
# Best available typical case
typ_foc <- smmr(results = sol_yp, 
     outcome = "EXPORT", 
     match=FALSE, 
     cases=2, 
     term = 3, 
     nec.cond = "STOCK")
typ_foc
```

The best available pairs - for each focal conjunct separately - of a typical and an iir case look as follows.

```{r chap 4 typiir}
# Best available pair of typical and iir cases
typiir_foc <- smmr(results = sol_yp, 
     outcome = "EXPORT", 
     match=TRUE, 
     cases=2, 
     term = 3, 
     nec.cond = "STOCK",
     max_pairs = 10)
typiir_foc
```

And the best pair of two typical cases is listed below.

```{r chap 4 typtyp}
# Best available pair of two typical cases
typtyp_foc <- smmr(results = sol_yp, 
              outcome = "EXPORT", 
              match=TRUE, 
              cases=1, 
              term = 3, 
              nec.cond = "STOCK")
typtyp_foc
```


# Chapter 5: @Schneider2014

In chapter 5, the example of @Schneider2014 is used for empirically illustrating different scenarios on the location of a case's membership in the within-case mechanism vis-a-vis its membership in the cross-case conditions and the outcome in a setting of full causal complexity (that is, a QCA solution that consists of a disjunction of conjunctions).

I start with model-refining, descriptive inference SMMR and then turn to causal inference SMMR.

```{r chap 5 data and mostpars}
MACRO.d <- read.csv('SchneiderMakszin2014.csv', row.names = 1)

# conditions
conds <- c("WC", "UN", "EP", "LM")

# truth table
tt_y <- truthTable(data = MACRO.d,
                   outcome = 'LPI',
                   conditions = conds,
                   incl.cut = 0.8,
                   sort.by = c('OUT', 'incl'),
                   complete = TRUE,
                   show.cases = TRUE)
tt_y

# most parsimonious solution
sol_yp <- minimize(input = tt_y,
                   include = '?',
                   details = TRUE)
sol_yp

```

```{r chap 5 dcov}
# missing disjuncts
dcov <- smmr(results = sol_yp,
               outcome = 'LPI',
               match = FALSE,
               cases = 4)
dcov
```

Eight truth table rows populated by deviant coverage cases. This signals high heterogeneity in data - something also illustrated by fact that there is just one remainder row (see truth table in chapter 1 ???). 
Another interesting observation: truth table row 2 (0001) has only deviant coverage case (NZ00) but no iir case. Why is the row not consistent enough to be included in the logical minimization? Let's plot it.

```{r chap 5 ttrow pimplot}
pimplot(data = MACRO.d,
        outcome = 'LPI',
        ttrows = c(2),
        results = sol_yp,
        all_labels = TRUE,
        jitter = TRUE,
        consH = TRUE)
```

The low consistency (.686) seems to be driven down by iir cases AND by skewed membership: with most cases being ttrow2 = 0, the few cases that are >0 have relatively more weight in the consistency formula.


Pairs of deviant coverage and iir cases:

```{r chap 5 dvoc-iir}
dcov_iir <- smmr(results = sol_yp,
                 outcome = 'LPI',
                 match = TRUE,
                 cases = 4)
dcov_iir
```

There are pairs of deviant coverage and iir cases in seven truth table rows (all but 0001 with NZ00).

## Descriptive inference SMMR

For identifying missing conjuncts, we need to select among deviant consistency cases, either alone or in comparision with a typical case.

```{r chap 5 dcons}
dcons <- smmr(results = sol_yp,
                outcome = 'LPI',
                match = FALSE,
                cases = 3)
dcons
```

Case AT05 deviates in all 3 sufficient terms (and AT00 in 2 of them). This might indicate an idiosyncratic reason? For instance, membership in the outcome of AT00 and AT05 could be assigned erroneously (e.g. typo in the data).

Comparing deviant consistency and typical cases, we obtain the following list of best-available pairs:

```{r chap 5 typ-devcons}
typdcons <- smmr(results = sol_yp,
                  outcome = 'LPI',
                  match = TRUE,
                  cases = 3)
typdcons
```

Note that only for one out of three terms do the most typical and most deviant pair also form the best available pair (ES95-AT00 for term $EP*LM$).


## Causal inference SMMR

### Single-case designs

All causal inference SMMR designs involve at least one typical case. Let's start with the single typical case SMMR design. Two options exist: either we focus on the entire term or on each conjunct in a term.

```{r chap 5 typ-term}
typ_term <- smmr(results = sol_yp,
              outcome = 'LPI',
              match = FALSE,
              cases = 1)
typ_term
```

Focusing on the entire term is recommended only if there are theoretical and conceptual reasons to treat 
the term as one set. Short of this, each conjunct needs to be focussed on separately for causal inference.

```{r chap 5 typ-foc}
typ_foc1 <- smmr(results = sol_yp,
              outcome = 'LPI',
              match = FALSE,
              cases = 2,
              term = 1)
typ_foc1

typ_foc2 <- smmr(results = sol_yp,
                 outcome = 'LPI',
                 match = FALSE,
                 cases = 2,
                 term = 2)
typ_foc2

typ_foc3 <- smmr(results = sol_yp,
                 outcome = 'LPI',
                 match = FALSE,
                 cases = 2,
                 term = 3)
typ_foc3
```

For all three terms and all conjuncts, close to ideal cases can be selected: uniquely covered, rank 1, consistent FC. Note that rarely are they the most typical for the term or FC, though, which is not a problem as such.

### Comparative-case designs

To obtain the best available pairs of typical and iir cases when treating a conjunction as one set, the following command needs to be run.

```{r chap 5 typiir-term}
typ_iir_term <- smmr(results = sol_yp,
                     outcome = 'LPI',
                     match = TRUE,
                     cases = 6,
                     max_pairs = 50)
typ_iir_term
```
We see that for each term, several pairs tick the important boxes of `UniqCov` and `GlobUncov`.

Matching two typical cases when treating the conjunction as one set, generates the following results:

```{r chap 5 typtyp-term}
typ_typ_term <- smmr(results = sol_yp,
                     outcome = 'LPI',
                     match = TRUE,
                     cases = 5,
                     max_pairs = 50)
typ_typ_term
```
For each of the three terms, pairs of uniquely covered typical cases can be found. 

In the book I do not treat each conjunct as one set and, instead, apply the focal conjunct principle. I discuss only two of the three conjunctions: $LM*WC$ and $LM*EP$. Let's start with $LM*WC$.

```{r chap 5 typiir LMWC}
typ_typ_foc1 <- smmr(results = sol_yp,
                      outcome = 'LPI',
                      match = TRUE,
                      cases = 1,
                      term = 1,
                      max_pairs = 50)
typ_typ_foc1
```

For focal conjunct $WC$: only one pair with both typical cases uniquely covered (DE95- DK00)

For focal conjunct $LM$: only pair in rank 2 with both uniquely covered (IE00 - DK00), but DK00 has inconsistent focal conjunct.

```{r chap 5 typiir LMEP}
typ_typ_foc3 <- smmr(results = sol_yp,
                      outcome = 'LPI',
                      match = TRUE,
                      cases = 1,
                      term = 3,
                      max_pairs = 200)
typ_typ_foc3
```
For focal conjunct $EP$ several pairs are fine (all involving case SE). The best-available pair even matches SE00 to SE05.

For focal conjunct $LM$ only on pair from rank 1 with both uniquely covered and consistent focal conjuncts exists (FR0 - SI95).


## Illustrating mechanisms

In the book, I empirically illustrate the location of the mechanism $M$ vis-a-vis the two conjunctions $LM*WC$ and $LM*EP$. More often than not, data on the best-available pairs of typical and iir cases is not available and therefore the next best-available case pairs need to be chosen. The following lines of command produce the graphs depicted in chapter 5 involving membership scores of cases in the mechanism.

```{r chap 5 load micro-macro data}
MECH <- read.csv('micro_macro_best.csv', row.names = 1)
```

Let's start with term $LM*WC$ and there with focal conjunct $WC$.

```{r chap 5 term WC*LM, WC smaller Y DE95  PL95}
MECH_test <- subset(MECH,
                    MECH$ID_mac %in%
                      c('DE95', 'PL95'))

with(MECH_test, plot(WC,LPI, 
                     pch = 16, cex = .7,
                     xlim = c(0,1),
                     ylim = c(0,1),
                     xlab = "cross-case focal conjunct WC and within-case mechanisms"))
text(MECH_test$WC, MECH_test$LPI, labels = MECH_test$ID_mac, 
     cex = 0.7, pos = 3)
legend(x = 0.5, y = 0.5, #"bottomright",                
       # Add legend to plot
       legend = c("WC", "lodif_repres", "lodif_poleff"),
       pch = c(16, 0, 1),
       cex = 0.5
)

abline(h=0.5, col="black")
abline(v=0.5, col="black")
abline(coef = c(0,1))

with(MECH_test, points(MECH_test$lorepres_diff,LPI, 
                       pch = 0, cex = .7))
with(MECH_test, points(MECH_test$lopoleff_diff,LPI, 
                       pch = 1, cex = .7))

```

And now focal conjunct $LM$ in conjunction $LM*WC$

```{r chap 5 LM*WC, LM smaller Y IE00 HU00}
# get only two relevant cases
MECH_test <- subset(MECH,
                    MECH$ID_mac %in%
                      c('IE00', 'HU00'))


{with(MECH_test, plot(LM,LPI, 
                     pch = 16, cex = .7,
                     xlim = c(0,1),
                     ylim = c(0,1),
                     xlab = "cross-case focal conjunct LM and within-case mechanisms"))
text(MECH_test$LM, MECH_test$LPI, labels = MECH_test$ID_mac, 
     cex = 0.7, pos = 3)
legend(x = 0.5, y = 0.5, #"bottomright",                
       # Add legend to plot
       legend = c("LM", "lodif_repres", "lodif_poleff"),
       pch = c(16, 0, 1),
       cex = 0.5
)

abline(h=0.5, col="black")
abline(v=0.5, col="black")
abline(coef = c(0,1))

with(MECH_test, points(MECH_test$lorepres_diff,LPI, 
                       pch = 0, cex = .7))
with(MECH_test, points(MECH_test$lopoleff_diff,LPI, 
                       pch = 1, cex = .7))}
```

Turning to conjunction $LM*EP$, let's focus first on conjunct $LM$.

```{r chap 5 LM*EP,  LM smaller Y FR05 RO00}
MECH_test <- subset(MECH,
                    MECH$ID_mac %in%
                      c('FR05', 'RO00'))

with(MECH_test, plot(LM,LPI, 
                     pch = 16, cex = .7,
                     xlim = c(0,1),
                     ylim = c(0,1),
                     xlab = "cross-case focal conjunct LM and within-case mechanisms"))
text(MECH_test$LM, MECH_test$LPI, labels = MECH_test$ID_mac, 
     cex = 0.7, pos = 3)
legend(x = 0.5, y = 0.5, #"bottomright",                
       # Add legend to plot
       legend = c("LM", "lodif_repres", "lodif_poleff"),
       pch = c(16, 0, 1),
       cex = 0.5
)

abline(h=0.5, col="black")
abline(v=0.5, col="black")
abline(coef = c(0,1))

with(MECH_test, points(MECH_test$lorepres_diff,LPI, 
                       pch = 0, cex = .7))
with(MECH_test, points(MECH_test$lopoleff_diff,LPI, 
                       pch = 1, cex = .7))
```

And finally, focal conjunct $EP$.

```{r chap 5 LM*EP EP smaller Y SE05 HU00}
# term LM*EP focal conjunct EP < Y: SE05 - HU00

MECH_test <- subset(MECH,
                    MECH$ID_mac %in%
                      c('SE05', 'HU00'))

with(MECH_test, plot(EP,LPI, 
                     pch = 16, cex = .7,
                     xlim = c(0,1),
                     ylim = c(0,1),
                     xlab = "cross-case focal conjunct EP and within-case mechanisms"))
text(MECH_test$EP, MECH_test$LPI, labels = MECH_test$ID_mac, 
     cex = 0.7, pos = 3)
legend(x = 0.5, y = 0.5, #"bottomright",                
       # Add legend to plot
       legend = c("EP", "lodif_repres", "lodif_poleff"),
       pch = c(16, 0, 1),
       cex = 0.5
)

# plot the mechanism dots
abline(h=0.5, col="black")
abline(v=0.5, col="black")
abline(coef = c(0,1))

with(MECH_test, points(MECH_test$lorepres_diff,LPI, 
                       pch = 0, cex = .7))
with(MECH_test, points(MECH_test$lopoleff_diff,LPI, 
                       pch = 1, cex = .7))
```


# Chapter 6: @Bretthauer2015

In chapter 6, I use the study by @Bretthauer2015 on peace and conflict in resource-scarse countries to illustrate SMMR on necessity claims of various levels of complexity ($A \Leftarrow Y$, $A + B \Leftarrow Y$, and $A * B \Leftarrow Y$).


*Outcome*

* BDT = Conflict

*Conditions*

* `PIN` = Low Quality of Political Institutions

* `COR` = Political corruption

* `EEX` = Ethnic exclusion

* `POV` = Poverty

* `DEP` = Dependence on agriculture

* `EDU` = High tertiary education

* `ECOD` = Economic development


```{r chap 6 load data}
#load data
BRETT <- read.csv("Bretthauer_2014_fs.csv", row.names =1)

colnames(BRETT) <- toupper(colnames(BRETT))
```


## $A \Leftarrow Y$

First, we identify the necessary condition $\sim DEP$ and visualize it via an X plot.

```{r chap 6 single nec}
# Analysis of single necessity, outcome ~Y
NEC_ny <- superSubset(data = BRETT,
                      outcome = '~BDT',
                      conditions = c("POV", "DEP", "COR",  "PIN",  "EDU",  "EEX"),
                      incl.cut = 0.89,
                      ron.cut = 0.6,
                      cov.cut = 0.5,
                      depth = 1)
NEC_ny

# Visualization via XY plot
pimplot(data = BRETT,
        outcome = 'BDT',
        results = NEC_ny,
        necessity = TRUE,
        all_labels = TRUE,
        jitter = TRUE)
```

### Descriptive inference SMMR

In the following, I show the four single-case and comparative SMMR designs with the goal of enhancing descriptive inferences.

I start with the best available deviant relevance case.

```{r chap 6 suffsol object}
# For technical reasons, a solution object from function minimize() is needed, even if one is not interested in an analysis of sufficiency. I therefore produce a truth table analysis of sufficiency, outcome ~Y
sol_np <- minimize(BRETT, 
                    outcome = "~BDT",
                    conditions = c("POV", "DEP", "ECOD",  "COR",  "PIN",  "EDU",  "EEX"),
                    incl.cut = 0.75,
                    pri.cut = 0.5,
                    details = TRUE, 
                    include = "?", 
                    row.dom = TRUE)
```


```{r chap 6 drel}
# Deviant relevance
drel <- smmr(nec.cond = "~DEP", 
             results = sol_np, 
             outcome = "~BDT", 
             match = FALSE, 
             cases = 3, 
             necessity = TRUE)
drel
```


The best pairs of typical - deviant relevance cases are listed here.
```{r chap 6 typdrel}
# typical - deviant relevance
typdrel <- smmr(nec.cond = "~DEP", 
              results = sol_np, 
              outcome = "~BDT", 
              match = TRUE, 
              cases = 1, 
              max_pairs = 10, 
              necessity = TRUE)
typdrel
```


The best available deviant consistency cases are shown as below. As could be seen from the XY plot, there is just one such case in the data: Bhutan. What the output adds to this is information of the truth table row to which Bhutan belongs.
```{r chap 6 dcons}
# deviant consistency
dcons <- smmr(nec.cond = "~DEP", 
              results = sol_np, 
              outcome = "~BDT", 
              match = FALSE, 
              cases = 2, 
              necessity = TRUE)
dcons
```

The best available pair of typical and deviant consistency cases is shown below. As we see, no typical case shares with Bhutan the same truth table (minus the necessary condition $\sim DEP$, of course, in which both case types hold different membership scores, by definition).
```{r chap 6 typdcons}
# typical - deviant consistency
typdcons <- smmr(nec.cond = "~DEP", 
                 results = sol_np, 
                 outcome = "~BDT", 
                 match = TRUE, 
                 cases = 2, 
                 max_pairs = 10, 
                 necessity = TRUE)
typdcons
```


### Causal inference SMMR

Now I turn to the three causal inference SMMR designs.

I start with a single typical case.

```{r chap 6 typ}
# typical case
typ <- smmr(nec.cond = "~DEP", 
            results = sol_np, 
            outcome = "~BDT", 
            match = FALSE, 
            cases = 1, 
            necessity = TRUE)
typ
```


The best-matching pairs of typical and iir cases are listed below.
```{r chap 6 typiir}
# typical - iir
typiir <- smmr(nec.cond = "~DEP", 
               results = sol_np, 
               outcome = "~BDT", 
               match = TRUE, 
               cases = 3, 
               max_pairs = 10, 
               necessity = TRUE)
typiir
```


Finally, the list best-matching pairs of two typical cases looks as follows.
```{r chap 6 typtyp}
# two typical cases
typtyp <- smmr(nec.cond = "~DEP", 
               results = sol_np, 
               outcome = "~BDT", 
               match = TRUE, 
               cases = 4, 
               max_pairs = 10, 
               necessity = TRUE)
typtyp
```

## $A + B \Leftarrow Y$

For illustrating SMMR in the presence of SUIN conditions, I continue with the example by @Bretthauer2015. Now the outcome consists in the presence of conflict ($BDT$). The necessity claim consisting of SUIN conditions looks as follows:


```{r chap 6 SUIN}
# Analysis of SUIN necessity, outcome BDT
SUIN_y <- superSubset(data = BRETT,
                     outcome = 'BDT',
                     conditions = c("POV", "DEP", "COR",  "PIN",  "EDU",  "EEX"),
                     incl.cut = 0.94,
                     ron.cut = 0.6,
                     cov.cut = 0.5,
                     depth = 2)
SUIN_y

#Visualize via XY plot
BRETT$POVCOR <- pmax(BRETT$POV, BRETT$COR)

xy.plot(data = BRETT,
        'POVCOR', 'BDT',
        necessity = TRUE,
        xlab = 'POV + COR',
        ylab = 'BDT',
        jitter = TRUE)
```

### Descriptive inference SMMR

I start with descriptive inference SMMR designs, and here with the study of a deviant relevance case. As can be seen, they are automatically listed for each SUIN condition and for the disjunction fo SUIN conditions. This holds for all other SMMR designs involving SUIN conditions.

```{r chap 6 SUIN suffsol Y}
# For technical reasons, a solution object from function minimize() is needed, even if one is not interested in an analysis of sufficiency. I therefore produce a truth table analysis of sufficiency, outcome ~Y

sol_p<- minimize(BRETT, outcome = "BDT",
                   conditions = c("POV", "DEP", "ECOD",  "COR",  "PIN",  "EDU",  "EEX"),
                 incl.cut1 = 0.75,
                 pri.cut = 0.5,
                 include = "?",
                 details = TRUE,
                 row.dom = TRUE)
```


```{r chap 6 SUIN drel}
# deviant relevance
drel <- smmr(nec.cond = "POV+COR", 
             results = sol_p, 
             outcome = "BDT", 
             match = FALSE, 
             cases = 3, 
             necessity = TRUE)
drel
```

The best-matching pairs of typical and deviant relevance cases are the following. 

```{r chap 6 SUIN typdrel}
# typical - deviant relevance
typdrel <- smmr(nec.cond = "POV+COR", 
                results = sol_p, 
                outcome = "BDT", 
                match = TRUE, 
                cases = 1,
                max_pairs = 6,
                necessity = TRUE)
typdrel
```

Deviant consistency cases are listed for each truth table row that contains at least one such case type. While there are deviant consistency cases for $POV$ and $COR$, respectively, there are none for the disjunction $POV+COR$. 

```{r chap 6 SUIN dcons}
# deviant Consistency
dcons <- smmr(nec.cond = "POV+COR", 
              results = sol_p, 
              outcome = "BDT", 
              match = FALSE, 
              cases = 2, 
              necessity = TRUE)
dcons
```

The comparison between typical and deviant consistency cases reveals that no such pair exists, except for one truth table row. The pair Eritrea - Haiti shares the same truth table row except for, by definition, membership in the focal disjunct $COR$.

```{r chap 6 SUIN typdcons}
# typical - deviant consistency
typdcons <- smmr(nec.cond = "POV+COR", 
                 results = sol_p, 
                 outcome = "BDT", 
                 match = TRUE, 
                 cases = 2,
                 max_pairs = 10,
                 necessity = TRUE)
typdcons
```


### Causal inference SMMR

The best available typical case for each focal SUIN condition and for the disjunction are listed as follows.

```{r chap 6 SUIN typ}
# TYP
typ <- smmr(nec.cond = "POV+COR", 
            results = sol_p, 
            outcome = "BDT", 
            match = FALSE, 
            cases = 1, 
            necessity = TRUE)
typ
```

The best-matching pairs of typical and iir cases are shown below.

```{r chap 6 SUIN typiir}
# TYP - IIR
typiir <- smmr(nec.cond = "POV+COR", 
               results = sol_p, 
               outcome = "BDT", 
               match = TRUE, 
               cases = 3,
               max_pairs = 7,
               necessity = TRUE)
typiir
```

And the best pairs of two typical cases are the following.

```{r chap 6 SUIN typtyp}
# TYP - TYP
typtyp <- smmr(nec.cond = "POV+COR", 
               results = sol_p, 
               outcome = "BDT", 
               match = TRUE, 
               cases = 4,
               max_pairs = 10,
               necessity = TRUE)
typtyp
```


## $A * B \Leftarrow Y$

I illustrate SMMR in the presence of a necessary conjunction with outcome $BDT$ from the study by @Bretthauer2015. For illustrative purposes, let us accept the conjunction $\sim ECOD * \sim EDU$ as a necessary conjunction for outcome $BDT$.

```{r chap 6 ININ sol conjunction}
# parameters of fit for conjunction
NEC_conj <- pof(data = BRETT, 
    ~ECOD * ~EDU <- BDT)

NEC_conj

# create conjunction set in data set
BRETT$ECODEDU <- pmin(1-BRETT$ECOD, 1-BRETT$EDU)

# XY plot for conjunction
xy.plot(data = BRETT,
        'ECODEDU', 'BDT',
        necessity = TRUE,
        xlab = '~ECOD*~EDU',
        ylab = 'BDT',
        jitter = TRUE)
```


### Descriptive inference SMMR

As before, I start with the four descriptive inference SMMR designs. Since the focal conjunct principle does not apply to descriptive inference SMMR designs, the output only shows cases for the conjunction, not each ININ condition separately. 

As can be seen, there are only two deviant relevance cases.

```{r chap 6 ININ drel}
# Deviant Rel.
drel <- smmr(nec.cond = "~ECOD*~EDU", 
            results = sol_p, 
            outcome = "BDT", 
            match = FALSE, 
            cases = 3, 
            necessity = TRUE)
drel
```

There are more pairs of typical and deviant relevance cases, all of them involving, of course, the two deviant relevance cases of either Cape Verde and Jordan.

```{r chap 6 ININ typdrel}
# TYP - DREL
typdrel <- smmr(nec.cond = "~ECOD*~EDU", 
                results = sol_p, 
                outcome = "BDT", 
                match = TRUE, 
                cases = 1,
                max_pairs = 10,
                necessity = TRUE)
typdrel
```

There are also just three deviant consistency cases. All three sit in different truth table rows.

```{r chap 6 ININ dcons}
# Deviant Cons.
dcons <- smmr(nec.cond = "~ECOD*~EDU", 
              results = sol_p, 
              outcome = "BDT", 
              match = FALSE, 
              cases = 2, 
              necessity = TRUE)
dcons
```

As the output below shows, no typical case shares the same truth table row with any of the three deviant consistency cases.

```{r chap 6 ININ typdcons}
# TYP - DCN
typdcons <- smmr(nec.cond = "~ECOD*~EDU", 
                 results = sol_p, 
                 outcome = "BDT", 
                 match = TRUE, 
                 cases = 2,
                 max_pairs = 10,
                 necessity = TRUE)
typdcons
```


### Causal inference SMMR

For causal inference designs, the focal conjunct principle applies and the best available (pairs of) cases are listed for each ININ cause and the necessary conjunction separately.

Single typical cases are listed below.

```{r chap 6 ININ typ}
# TYP
typ <- smmr(nec.cond = "~ECOD*~EDU", 
            results = sol_p, 
            outcome = "BDT", 
            match = FALSE, 
            cases = 1, 
            necessity = TRUE)
typ
```

The best-matching pairs of typical and iir cases look as follows.

```{r chap 6 ININ typiir}
# TYP - IIR
typiir <- smmr(nec.cond = "~ECOD*~EDU", 
               results = sol_p, 
               outcome = "BDT", 
               match = TRUE, 
               cases = 3,
               max_pairs = 10,
               necessity = TRUE)
typiir
```

And the best available pairs of typical cases are listed below.

```{r chap 6 ININ typtyp}
# TYP - TYP
typtyp <- smmr(nec.cond = "~ECOD*~EDU", 
               results = sol_p, 
               outcome = "BDT", 
               match = TRUE, 
               cases = 4,
               max_pairs = 7,
               necessity = TRUE)
typtyp
```



***

# References {-}

<div id="refs"></div>
